﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Principal;

namespace IceTea.Domain
{
    /// <summary>
    /// Customize the IPrincipal
    /// </summary>
    public class IceTeaPrincipal : IPrincipal
    {
        private IPrincipal _principal;
        
        /// <summary>
        /// Store the project and team roles profile
        /// </summary>
        public RoleProfile RoleProfile
        { get; set; }

       
        public IceTeaPrincipal(IPrincipal principal)
        {
            _principal = principal;
        }

        #region Implement IPrincipal 
        public IIdentity Identity
        {
            get { return _principal.Identity; }
        }

        public bool IsInRole(string role)
        {
            return _principal.IsInRole(role);
        }

        /// <summary>
        /// Used by authorization, to check whether current user has a specified role
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool IsInRole(SecurityRole role)
        {
            //whether is a super admin
            if ((role & SecurityRole.SuperAdmin) != 0)
            {
                if (RoleProfile.IsSuperAdmin)
                    return true;
            }
            //whether is admin of the current project
            if ((role & SecurityRole.ProductAdmin) != 0)
            {
                if (IsInCurrentProduct()
                    && RoleProfile.ProductProfiles[RoleProfile.CurrentProductId].IsAdmin)
                    return true;
            }
            
            //whether is product owner of current project
            if ((role & SecurityRole.ProductOwner) != 0)
            {
                if (IsInScrumRole(ScrumRole.ProductOwner))
                    return true;
            }
            //whether is Scrum master of current product
            if ((role & SecurityRole.ScrumMaster) != 0)
            {
                if (IsInScrumRole(ScrumRole.ScrumMaster))
                    return true;
            }
            //whether is Development team member 
            if ((role & SecurityRole.DevelopmentTeam) != 0)
            {
                if (IsInScrumRole(ScrumRole.DevelopmentTeam))
                    return true;
            }
            //whether is stakeholder 
            if ((role & SecurityRole.Stakeholder) != 0)
            {
                if (IsInScrumRole(ScrumRole.Stakeholder))
                    return true;
            }
            //whether is valid user
            if ((role & SecurityRole.ValidUser) != 0)
            {
                return true;
            }
            return false;
        }

        protected bool IsInCurrentProduct()
        {
            return RoleProfile.ProductProfiles.ContainsKey(RoleProfile.CurrentProductId);
        }

        protected bool IsInScrumRole(ScrumRole role)
        {
            if (IsInCurrentProduct())
            {
                if ((from tp in RoleProfile.ProductProfiles[RoleProfile.CurrentProductId].TeamProfiles.Values
                     where tp.Roles.Contains((int)role)
                     select tp).Count() > 0)
                    return true;
            }
            return false;
        }
        #endregion
    }
}
